#include "tableresizedialog.h"
#include <QVBoxLayout>
#include <QHBoxLayout>
#include <QGridLayout>

TableResizeDialog::TableResizeDialog(int columns, int rows, QWidget *parent)
    : QDialog(parent)
{
    setWindowTitle("调整表格行列宽");

    setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
    setModal(true);

    // 列宽
    columnWidthCheckBox = new QCheckBox("调整列宽:");
    columnWidthSpinBox = new QSpinBox();
    columnWidthSpinBox->setRange(10, 500);
    columnWidthSpinBox->setValue(100);
    columnWidthSpinBox->setSuffix(" px");
    columnWidthSpinBox->setEnabled(false);

    // 行高
    rowHeightCheckBox = new QCheckBox("调整行高:");
    rowHeightSpinBox = new QSpinBox();
    rowHeightSpinBox->setRange(10, 500);
    rowHeightSpinBox->setValue(30);
    rowHeightSpinBox->setSuffix(" px");
    rowHeightSpinBox->setEnabled(false);

    // 按钮
    okButton = new QPushButton("确定");
    cancelButton = new QPushButton("取消");

    // 连接信号和槽
    connect(columnWidthCheckBox, &QCheckBox::toggled, columnWidthSpinBox, &QSpinBox::setEnabled);
    connect(rowHeightCheckBox, &QCheckBox::toggled, rowHeightSpinBox, &QSpinBox::setEnabled);
    connect(okButton, &QPushButton::clicked, this, &QDialog::accept);
    connect(cancelButton, &QPushButton::clicked, this, &QDialog::reject);

    // 表格信息
    QLabel *tableInfoLabel = new QLabel(QString("当前表格大小: %1 行 × %2 列").arg(rows).arg(columns));

    // 创建布局
    QGridLayout *formLayout = new QGridLayout();
    formLayout->addWidget(tableInfoLabel, 0, 0, 1, 2);
    formLayout->addWidget(columnWidthCheckBox, 1, 0);
    formLayout->addWidget(columnWidthSpinBox, 1, 1);
    formLayout->addWidget(rowHeightCheckBox, 2, 0);
    formLayout->addWidget(rowHeightSpinBox, 2, 1);

    QHBoxLayout *buttonLayout = new QHBoxLayout();
    buttonLayout->addStretch();
    buttonLayout->addWidget(okButton);
    buttonLayout->addWidget(cancelButton);

    QVBoxLayout *mainLayout = new QVBoxLayout();
    mainLayout->addLayout(formLayout);
    mainLayout->addLayout(buttonLayout);

    setLayout(mainLayout);

    // 设置窗口大小
    resize(300, 150);
}

int TableResizeDialog::columnWidth() const
{
    return columnWidthSpinBox->value();
}

int TableResizeDialog::rowHeight() const
{
    return rowHeightSpinBox->value();
}

bool TableResizeDialog::hasColumnWidth() const
{
    return columnWidthCheckBox->isChecked();
}

bool TableResizeDialog::hasRowHeight() const
{
    return rowHeightCheckBox->isChecked();
}
